package com.atguigu.Base;


//在高并发低耦合的情况下。->线程  操作 资源类


class Ticket {      //对象=filed+方法
    private int number = 50;

    public synchronized void sale()
    {
        if(number > 0)
        {
            System.out.println(Thread.currentThread().getName()+"\t"+"卖出第："+(number--)+"\t 还剩下："+number);
        }
    }
}


public class SaleTicketDemoV1 {
    public static void main(String[] args) {//main只是程序的入口

        Ticket ticket = new Ticket();
//==========================================================================
        //Thread(Runnable target, String name) Allocates a new Thread object.构造方法
        new  Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i <= 50; i++){
                    ticket.sale();
                }
            }
        },"A").start();
//==========================================================================
        new  Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i <= 50; i++){
                    ticket.sale();
                }
            }
        },"B").start();
//==========================================================================
        new  Thread(()->{for (int i = 0; i <= 50; i++)ticket.sale();},"C").start();

    }
}
